package ke08;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;

public class SWTGui {

	public static void main(String[] args) {

		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("Meine SWT GUI");

		GridLayout gridLayout = new GridLayout();
		shell.setLayout(gridLayout);

		Label caption = new Label(shell, SWT.NONE);
		caption.setText("This game sucks!");
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		caption.setLayoutData(gridData);

		final Group group = new Group(shell, SWT.BORDER_SOLID);
		group.setText("COUNT MACHINE");
		gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
		gridData.heightHint = 150;
		group.setLayoutData(gridData);

		GridLayout groupGrid = new GridLayout();
		groupGrid.numColumns = 3;
		groupGrid.makeColumnsEqualWidth = true;
		group.setLayout(groupGrid);

		final Button inc = new Button(group, SWT.PUSH);
		inc.setText("INCREMENT");
		final Label counter = new Label(group, SWT.NONE);

		final Button dec = new Button(group, SWT.PUSH);
		dec.setText("DECREMENT");
		inc.setLayoutData(gridData);
		dec.setLayoutData(gridData);

		counter.setLayoutData(gridData);
		counter.setText("50");
		gridData = new GridData();
		gridData.horizontalAlignment = SWT.CENTER;
		gridData.verticalAlignment = SWT.CENTER;
		counter.setLayoutData(gridData);

		FontData fontData = counter.getFont().getFontData()[0];
		fontData.setHeight(40);
		fontData.setStyle(SWT.BOLD);
		counter.setFont(new Font(null, fontData));

		/* actions */
		counter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				counter.setText("50");
			}
		});

		SelectionListener changeCounter = new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int current = Integer.parseInt(counter.getText());
				int update = current + 1 * (e.getSource() == inc ? 1 : -1);
				counter.setText(String.valueOf(update));
			}
		};
		inc.addSelectionListener(changeCounter);
		dec.addSelectionListener(changeCounter);

		shell.addControlListener(new ControlAdapter() {

			private Point prevSize = shell.getSize();

			@Override
			public void controlResized(ControlEvent e) {
				if (shell.isVisible()) {
					Point sizeNow = shell.getSize();
					if (sizeNow.x * sizeNow.y > prevSize.x * prevSize.y) {
						counter.setText(String.valueOf(Integer.parseInt(counter
								.getText()) + 1));
					} else if (sizeNow.x * sizeNow.y < prevSize.x * prevSize.y) {
						counter.setText(String.valueOf(Integer.parseInt(counter
								.getText()) - 1));
					}
					prevSize = sizeNow;
					group.pack();
				}
			}
		});

		shell.pack();

		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle shellBounds = shell.getBounds();
		int x = bounds.x + (bounds.width - shellBounds.width) / 2;
		int y = bounds.y + (bounds.height - shellBounds.height) / 2;
		shell.setLocation(x, y);
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		shell.dispose();
	}
}
